package com.zjdiepu.www.manage.api.other;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.other.OtherBankRateVoMapper;
import com.zjdiepu.www.ibiz.other.IOtherBankRateService;
import com.zjdiepu.www.model.other.dto.OtherBankRate;
import com.zjdiepu.www.model.other.ext.OtherBankRateExt;

/**
 * other_银行利率维护Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年10月25日 14时11分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "OtherBankRateController", produces="application/json;charset=UTF-8")
@Api(tags = {"other_银行利率维护"}, produces="application/json;charset=UTF-8")
public class OtherBankRateController extends BaseController {
    @Resource
    private IOtherBankRateService otherBankRateService;
    @Resource
    private OtherBankRateVoMapper otherBankRateVoMapper;

    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增other_银行利率维护 <br/>
     * @param otherBankRate
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增other_银行利率维护", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute OtherBankRate otherBankRate) {
    	AbstractSysUser currentUser = getCurrentUser();
    	otherBankRate.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	otherBankRate.setModifierName(currentUser.getName());
//    	OtherBankRateExt otherBankRateExt = new OtherBankRateExt();
//    	otherBankRateExt.notDeleted();
//    	otherBankRateExt.setBankCode(otherBankRate.getBankCode());
//    	otherBankRateExt.setMonthNum(otherBankRate.getMonthNum());
//    	otherBankRateExt.setRows(2);
//    	List<OtherBankRate> otherBankRates = otherBankRateService.selectByExt(otherBankRateExt);
//    	boolean repeat = false;
//    	if(CollectionUtils.isNotEmpty(otherBankRates) && otherBankRates.size() > 1) {
//    		repeat = true;
//    	} else if(CollectionUtils.isNotEmpty(otherBankRates) && otherBankRates.size() == 1){
//    		OtherBankRate dbOtherBankRate = otherBankRates.get(0);
//    		repeat = !dbOtherBankRate.getId().equals(otherBankRate.getId());
//    	}
//    	if(repeat) {
//    		return ApiResponse.fail("999", "银行和贷款期数重复,请直接修改");
//    	}
    	if(StringUtils.isBlank(otherBankRate.getId())) {
    		otherBankRateService.saveUpdateAndDelete(otherBankRate, OperateType.add);
    	} else {
    		otherBankRateService.saveUpdateAndDelete(otherBankRate, OperateType.edit);
    	}
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除other_银行利率维护<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除other_银行利率维护", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	otherBankRateService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: disable  <br/>
     * @Description: 禁用other_银行利率维护<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用other_银行利率维护", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	otherBankRateService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: enable <br/>
     * @Description: 启用other_银行利率维护<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用other_银行利率维护", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	otherBankRateService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改other_银行利率维护 <br/>
     * @param otherBankRate
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改other_银行利率维护", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute OtherBankRate otherBankRate) {
    	AbstractSysUser currentUser = getCurrentUser();
    	otherBankRate.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	otherBankRate.setModifierName(currentUser.getName());
    	otherBankRateService.saveUpdateAndDelete(otherBankRate, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询other_银行利率维护<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询other_银行利率维护", response = OtherBankRate.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       OtherBankRate otherBankRate = otherBankRateService.selectById(id);
       if(null == otherBankRate) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(otherBankRate);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询other_银行利率维护 <br/>
     * @param otherBankRateExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询other_银行利率维护",response = OtherBankRate.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute OtherBankRateExt otherBankRateExt){
        otherBankRateExt.notDeleted();
        PageVo page = getPage(otherBankRateExt);
        int records = otherBankRateService.countByExt(otherBankRateExt);
        page.setRecords(records);
        if (records > 0) {
            List<OtherBankRate> otherBankRates = otherBankRateService.selectByExt(otherBankRateExt);
            otherBankRateVoMapper.mapper(otherBankRates);
            page.setRows(otherBankRates);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: listSelect  <br/>
     * @Description: 选择银行基准利率 <br/>
     * @param loanMonth
     * @param loanBank
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "选择银行基准利率",response = OtherBankRate.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "listSelect", method = RequestMethod.POST)
    public ApiResponse listSelect(Integer loanMonth, String loanBank){
    	OtherBankRateExt otherBankRateExt = new OtherBankRateExt();
    	otherBankRateExt.setBaseWhere();
        otherBankRateExt.notDeleted();
        otherBankRateExt.setMonthNum(loanMonth);
        otherBankRateExt.setBankCode(loanBank);
        PageVo page = getPage(otherBankRateExt);
        int records = otherBankRateService.countByExt(otherBankRateExt);
        page.setRecords(records);
        if (records > 0) {
            List<OtherBankRate> otherBankRates = otherBankRateService.selectByExt(otherBankRateExt);
            otherBankRateVoMapper.mapper(otherBankRates);
            page.setRows(otherBankRates);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
